﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapControl.Misc;
using MapControl.Misc.DAL;

namespace GPSMap
{
    public partial class frmAirportList : Form
    {
        private AirPortRepository _airPortRespository;

        public bool isChanged = false; //Cho biết tình trạng có thay đổi thông tin không
        public frmGPSTracking frmParent;
        public frmAirportList(frmGPSTracking frm)
        {
            InitializeComponent();
            frmParent = frm;
            HTLibrary.DataGridViewHelper.DataGridView_InitEvent(dataGridViewAirport);
            _airPortRespository = new AirPortRepository(GeneralClass.appDBFile);
        }

        private void frmAirport_Load(object sender, EventArgs e)
        {
            BindingDataGridView();
            FormatDataGridView();
        }

        /// <summary>
        /// Refresh lại datagridview sau khi thêm mới hoặc cập nhật
        /// </summary>
        /// <param name="isInsert"></param>
        public void RefreshDataGridView(bool isInsert)
        {
            if (isInsert)
            {
                BindingDataGridView(dataGridViewAirport.Rows.Count);
            }
            else
            {
                int indexRow = dataGridViewAirport.CurrentCell != null ? dataGridViewAirport.CurrentCell.RowIndex : 0;
                BindingDataGridView(indexRow);
            }
        }

        public void BindingDataGridView(int indexRow = -1)
        {
            DataTable db = _airPortRespository.GetAll();
            BindingSource bsAirport = new BindingSource();
            bsAirport.DataSource = db.DefaultView;
            dataGridViewAirport.DataSource = bsAirport;
            bindingNavigator1.BindingSource = bsAirport;
            
            if (indexRow > 0)
            {
                if (indexRow == dataGridViewAirport.Rows.Count)
                    indexRow--;
                foreach (DataGridViewColumn col in dataGridViewAirport.Columns)
                {
                    if (col.Visible)
                    {
                        dataGridViewAirport.CurrentCell = dataGridViewAirport.Rows[indexRow].Cells[col.Index];
                        break;
                    }
                }
            }
            else
            {
                if (dataGridViewAirport.Rows.Count > 0)
                {
                    dataGridViewAirport.Rows[0].Selected = true;
                }
            }

        }

        private void FormatDataGridView()
        {
            dataGridViewAirport.ReadOnly = true;

            dataGridViewAirport.Columns["airport_id"].Visible = false;
            dataGridViewAirport.Columns["airport_name"].HeaderText = "Tên sân bay";
            dataGridViewAirport.Columns["longitude"].HeaderText = "Kinh độ";
            dataGridViewAirport.Columns["latitude"].HeaderText = "Vĩ độ";
            dataGridViewAirport.Columns["isActive"].Visible = false;

            dataGridViewAirport.Columns["longitude"].DefaultCellStyle.Format = "#.0000000";
            dataGridViewAirport.Columns["longitude"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewAirport.Columns["latitude"].DefaultCellStyle.Format = "#.0000000";
            dataGridViewAirport.Columns["latitude"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int indexRow = dataGridViewAirport.CurrentCell != null ? dataGridViewAirport.CurrentCell.RowIndex : 0;
                string msg = string.Format("Bạn thật sự muốn xóa sân bay \"{0}\"?",
                                           dataGridViewAirport.Rows[indexRow].Cells["airport_name"].Value);
                if (MessageBox.Show(msg, "Thông báo" , MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    _airPortRespository.Delete(int.Parse(dataGridViewAirport.Rows[indexRow].Cells["airport_id"].Value.ToString()));
                    indexRow--;
                    BindingDataGridView(indexRow);
                    MessageBox.Show("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                frmAirport frm = new frmAirport(this);
                frm.Show();
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int indexRow = dataGridViewAirport.CurrentCell != null ? dataGridViewAirport.CurrentCell.RowIndex : 0;
                Airport obj = new Airport();
                obj.Airport_Id = int.Parse(dataGridViewAirport.Rows[indexRow].Cells["airport_id"].Value.ToString());
                obj.Airport_Name = dataGridViewAirport.Rows[indexRow].Cells["airport_name"].Value.ToString();
                obj.Longitude = double.Parse(dataGridViewAirport.Rows[indexRow].Cells["longitude"].Value.ToString());
                obj.Latitude = double.Parse(dataGridViewAirport.Rows[indexRow].Cells["latitude"].Value.ToString());
                frmAirport frm = new frmAirport(this, obj);
                frm.Show();
                BindingDataGridView(indexRow);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void dataGridViewAirport_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void frmAirportList_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmParent.RefreshAirportList(isChanged);
        }
    }
}